﻿@model VideoCache.Server.ViewModel.AlbumModel

@{
    ViewBag.Title = "Create";
}
@section script
{
<script type="text/javascript">
    var searchResult;
    function selectItem(index) {
        if (searchResult.length > index) {
            $("#Title").val(searchResult[index].Title);
            $("#TypeId").val(searchResult[index].TypeId);
            $("#AreaId").val(searchResult[index].AreaId);
            $("#Rating").val(searchResult[index].Rating);
            $("#Director").val(searchResult[index].Director);
            $("#Actor").val(searchResult[index].Actor);
            $("#PubYear").val(searchResult[index].PubYear);
            if (searchResult[index].TypeId == 2) {
                $("#IsEnd").attr("checked", true);
            }
            else {
                $("#IsEnd").attr("checked", searchResult[index].IsEnd);
            }
            $("#Priority").val(searchResult[index].Priority);
            //$("#Status").val(searchResult[index].Status);
            $("#Description").val(searchResult[index].Description);
            $("#BaiduId").val(searchResult[index].BaiduId);
        }
    }

    function searchBaidu() {
        var text = $("#Title").val();
        if (text.length > 0) {
            var data = {keyword : text };
            $.ajax({
                url: '@Url.Action("SearchFromBaidu")',
                type: "POST",
                data: data,
                success: function (result) {
                    $("#result").show();
                    searchResult = result;
                    for (var i = 0; i < searchResult.length; i++) {
                        var html = "<tr>";
                        html += "<td>" + searchResult[i].Title + "</td>";
                        html += "<td>" + searchResult[i].PubYear + "</td>";
                        html += "<td>" + searchResult[i].AreaId + "</td>";
                        html += "<td>" + searchResult[i].Director + "</td>";
                        html += "<td>" + searchResult[i].Actor + "</td>";
                        html += "<td><a href='javascript:selectItem(" + i + ")'>选择</a></td>";
                        html += "</tr>";
                        $("#searchResult").append(html);
                    }
                },
                error: function () {
                    alert("搜索发生错误。");
                }
            });
        }
    }
</script>
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
        <div class="unit">
            <div class="editor-label">
                @Html.LabelFor(model => model.Title)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Title, new { style="width:400px" })
                @Html.ValidationMessageFor(model => model.Title)
            </div>
             <div class="editor-field">
                <button type="button" id="btnSearch" onclick="searchBaidu()">从百度查找</button>
            </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.TypeId)
        </div>
        <div class="editor-field">
            @Html.DropDownList("TypeId", "请选择")
            @Html.ValidationMessageFor(model => model.TypeId)
        </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.AreaId)
        </div>
        <div class="editor-field">
            @Html.DropDownList("AreaId", "请选择")
            @Html.ValidationMessageFor(model => model.AreaId)
        </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.Rating)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Rating)
            @Html.ValidationMessageFor(model => model.Rating)
        </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.Actor)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Actor, new { style = "width:400px" })
            @Html.ValidationMessageFor(model => model.Actor)
        </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.Director)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Director, new { style="width:400px"})
            @Html.ValidationMessageFor(model => model.Director)
        </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.PubYear)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PubYear)
            @Html.ValidationMessageFor(model => model.PubYear)
        </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.IsEnd)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IsEnd)
            @Html.ValidationMessageFor(model => model.IsEnd)
        </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.Priority)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Priority)
            @Html.ValidationMessageFor(model => model.Priority)
        </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.BaiduId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BaiduId)
            @Html.ValidationMessageFor(model => model.BaiduId)
        </div>
        </div>
    
        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.Status)
        </div>
        <div class="editor-field">
            @Html.DropDownList("Status", "请选择")
            @Html.ValidationMessageFor(model => model.Status)
        </div>
        </div>

        <div class="unit">
        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Description, new { style = "width:400px", rows = "8" })
            @Html.ValidationMessageFor(model => model.Description)
        </div>
        </div>
    
        <div class="unit">
            <input type="submit" value="Create" />
            @Html.ActionLink("Back to List", "Index")
        </div>
}
<div class="unit" style="display:none" id="result">
<table width="100%" class="list">
<thead>
    <tr>
        <th width="200">视频名称</th>
        <th width="50">年代</th>
        <th width="50">地区</th>
        <th width="200">导演</th>
        <th width="200">主演</th>
        <th></th>
    </tr>
</thead>
<tbody id="searchResult">
    
</tbody>
</table>
</div>